lots of optmizations and better organization of ruby code