Refactored the code a bit. Fully used monadic interface.