Rails code refactor && bug fixes