Fix #331, remove duplicate code