Refactor; Relocate various functions