added phpdoc comments, refactored some code