Refactored a bit and removed some code ambiguity