Added comments and tried to follow c-standard