Improved legibility of code

Added comments, simplified or removed some parts of code.