Added some suggestions by Hannes and simplified some other code