cleaner and more stable implementation