More work on label positioning; code refactoring