Refactor code to make much more functional

Also makes the joint prefix variable