Implemented changes after feedback from @janos on SO.