set function order properly.
Cleaned up code, tried to match bash standards, cleared useless comments.