Also, refactor and simplify the corresponding functions (and finally remove the chat between long inactive developers in the comments!) Fix #316