Added exceptions check, various optimizations