Refactoring some funcs
Test fixes