Improve implementation, fix formatting