Various fixes

This PR contains some timeout fixes as well as some minor refactorings I did when reading through the code to understand it better.