BUGFIX: correct return values and clean up code