refactored a bit to use perl prepared statements