Added some little optimization and cleaned code