Refactor and comment a bit