Refactor code; also take optional filename and path from args