Implemented feature #174 and cleaned the code a bit.