Main function redone