Improve code structure and a few comments here and there