works correctly, though very ugly