fixed bugs, optimized, improved formatting