Simpler and more robust implementation.