bugfix and code cleanup