added final bug fixes and added complete comments