better error handling, bug fixing