changed some unsigned to int, cleaned and optimized the code