performance, style fixes, refactiring