Added more comments and changed function order to comply with specs