fixed some errors, optimized