Simplified the code a bit by reducing the level of monkey patching