Fix some bugs and extract static class to own files