Refactoring to get rid of partiality and auxilary functions