Added function comments and little refactoring