Add comments and improve script

Added comments for each part of the script.
Rewrite some of the inline commands as functions for better readability.
TODO: Add some more functionality and make the script easier to use.