Added some documentation and code refactoring