bugfix and error check