refactored for better label handling, bugfix