Cleaned up the source code and added few comments and docstrings.