Comments added, code refactored