Clarify obscure functions and remove debugging printfs