Cleaned up code. Added comments. Failure mechanism etc.