Lots of optimalisations

better error handling, better parameter checking, object instead of
hashtable