changed code in favor of better error and output handling