minor errors
more or less most efficient version i'm going to get with this code