Refactor main due to segfault