Refactoring
 - removed unused variables
 - more comments
 - one mainloop