Defactored complexity
Fixed issue causing unit test to fail