Add timeout logic and various cleanups