fixed some problems with spaces, optimized code