Renamed functions internally, reorganized, and documented