Code fixes and speed improvement