Improved documentation and cleaned up code