Minor code improvements

Added comments, renamed vars and functions, restructured, removed dead
code, made some minor code improvements