Fixed poorly implemented code