memory usage optimizations for read and write operations

1. Forward declaration for this function is missing.
2. We usually define functions after they've been used.