There is a lot of code duplication here