Refactored even more and added free variable checking