Refactor RTL code and fix #179