Change function order and improve commenting