Bug fixes and ReSharper optimizations (fully tested)