More refactoring, fixed type that caused an infinite loop, closes #48