Cleanup

Cleaned up code. Added comments etc.