Refactored a bit and removed some dead code.