refactor slightly, still not happy with the ammount of duplication