Refactor & correct some mistakes