Added additional helper functions and rewrote some of the old parts