Added documentation and simplified code