Improvements, small optimizations. Code is tested.