refactor; resolve memory issues