Moved some code out of some functions to slim it down and reduce the overloadedness.